<template lang="html">
  <div class="main-page" id="store-page">
    <!-- 标题 -->
    <!-- <van-nav-bar class="main-header" title="商学院" :left-arrow="true" :fixed="true" :z-index="11" @click-left="onClickLeft"/> -->
    <nav-bar :title="title"  @goBack="onClickLeft"/>

    <!-- 主体内容 -->
    <div class="main-content has-header-space">
      <!--pages/message_center/message_list/message_list.wxml-->
      <van-list :offset="offset" class="taem_datalist" v-model="loading" error-text="请求失败，点击重新加载" @load="onLoad()" loading-text="正在加载中" :finished="finished">
        <div class="message_list" v-if="msg_list.length">
          <Item :item_type="item_type" :item="item" v-for="(item, k) in msg_list" v-bind:key="k"></Item>
        </div>
        <NotData v-if="!msg_list.length && !loading" text="还没有消息哦" icon="icon_1"></NotData>
      </van-list>
    </div>
  </div>
</template>

<script>
import NavBar from '@/components/global/nav-bar.vue'
import Item from '@/components/merber/message/item.vue'
import NotData from '@/components/global/not-data.vue'

export default {
  components: { NavBar, Item, NotData },
  data () {
    return {
      msg_list: [],
      page: 0,
      message_classify: '',
      item_type: '',
      finished: false,
      loading: true,
      offset: 10,
      title: '消息中心'
    }
  },
  created () {
    this.message_classify = this.$route.params.id
    this.item_type = this.$route.params.id
    if (this.$route.params.id === '1') {
      this.title = '系统消息'
    } else if (this.$route.params.id === '4') {
      this.title = '佣金奖励'
    } else if (this.$route.params.id === '2') {
      this.title = '好货推荐'
    }
    this.getMsgList()
  },
  methods: {
    onClickLeft () {
      this.$router.go(-1)
    },
    onLoad () {
      this.page = this.page + 1
      this.getMsgList()
    },
    getMsgList () {
      var page = this.page
      var params = { page: page, pagesize: 10, message_classify: this.message_classify, type: this.item_type === '2' ? 'good' : true, sort: 'message_id', message_channel: 1 }
      return new Promise((resolve, reject) => {
        this.$Axios({ url: 'sns_user_message', params, method: 'get' }).then((resp) => { // 请求成功
          if (resp.return_code === '0000') {
            this.msg_list = this.msg_list.concat(resp.data.list)
            if (resp.data.list.length < 10) {
              this.finished = true
            }
          }
          this.loading = false
          resolve(resp)
        }).catch((error) => { // 请求失败
          this.$_requestInterceptor('error', error)
          reject(error)
        })
      })
    }
  }
}
</script>
